package com.example.wuqujingdianphone.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.adapter.BCBookAdapter;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.bean.BookListBean2;
import com.example.wuqujingdianphone.utils.NiceDialogUtils;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BookSearchActivity extends BaseActivity {
    private static final String TAG = "BookSearchActivity";
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edit_search01)
    EditText mEditSearch01;
    @BindView(R.id.edit_search02)
    EditText mEditSearch02;
    @BindView(R.id.btn_search)
    Button mBtnSearch;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private BCBookAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_search);
        ButterKnife.bind(this);
        mTitle.setText("图书搜索");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initView();

    }

    private void initView() {


        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mAdapter = new BCBookAdapter(this, null);
        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.i(TAG, "onItemClick: position=" + position);

                /*BookListBean2 item = mAdapter.getItem(position);
                BookDetailParcelable bookDetailBean = new BookDetailParcelable();
                bookDetailBean.id = item.id;
                bookDetailBean.spic = item.spic;
                bookDetailBean.title = item.news_tit;
                bookDetailBean.news_source = item.news_source;
                bookDetailBean.jianjie = item.jianjie;
                bookDetailBean.url = item.url;

                Intent intent = new Intent(getActivity(), BookDetailActivity.class);
                intent.putExtra("bookDetail", bookDetailBean);
                startActivity(intent);*/

                Intent intent = new Intent(BookSearchActivity.this, BookDetailActivity.class);
                intent.putExtra("bookId", mAdapter.getItem(position).id);
                intent.putExtra("bookTitle", mAdapter.getItem(position).news_tit);
                startActivity(intent);

            }
        });

        mBtnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String editSearchContext = mEditSearch01.getText().toString().trim();
                if (TextUtils.isEmpty(editSearchContext)) {
                    ToastUtil.show("搜索的内容不能为空！");
                } else {
                    //ToastUtil.show("开发中...");
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(mEditSearch01.getWindowToken(), 0); //强制隐藏键盘

                    getSearchDataFromServer(editSearchContext);
                }
            }
        });


    }


    private void getSearchDataFromServer(String searchContent) {
        showLoading(this);

        ViseHttp.GET("/index.php/Jiekou/search/keyword/" + searchContent)
                .tag(TAG)
                // .addParam("", searchContent)
                .request(new ACallback<List<BookListBean2>>() {


                    @Override
                    public void onSuccess(List<BookListBean2> bookListBeans) {
                        cancelLoading();
                        // Log.i(TAG, "onSuccess: bookListBeans.size=" + bookListBeans.size() + " " + bookListBeans.get(0).url);
                        if (bookListBeans != null && bookListBeans.size() > 0) {
                            mAdapter.setNewData(bookListBeans);
                        } else {
                            ToastUtil.show("没有您想要的内容！");
                            Log.i(TAG, "onSuccess: bookListBeans is null");
                        }

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        cancelLoading();
                        ToastUtil.show("errCode=" + errCode + "　errMsg＝" + errMsg);
                    }
                });


    }
}
